#pragma once

#include<BWAPI.h>
#include<set>

#include"ControlerBase.h"
#include"Controlers.h"

using namespace BWAPI;
using namespace std;

class DroneControler : public ControlerBase
{
public:
	DroneControler(Unit* drone)
		:ControlerBase(drone),
		_canWork(false)
	{
	}
	virtual void OnFrame()
	{
		if(IsDead())
		{
			return;
		}

		if(_canWork)
		{
			if(_state == UnKnown)
			{
				if(_unitToControl->isUnderAttack())
				{
					set<Unit*> attackers;
					for each(Unit* nearUnit in _unitToControl->getUnitsInRadius(100))
					{
						if(Broodwar->self()->isEnemy(nearUnit->getPlayer()))
						{
							attackers.insert(nearUnit);
						}
					}
					if(attackers.size() > 0)
					{
						Attack(*attackers.begin());
					}
				}
			}
			else if(_state == AttackUnit)
			{
				if(_unitToControl->getDistance(_target) > 150)
				{
					_unitToControl->stop();
					_state = UnKnown;
				}
				else
				{
					ControlerBase::OnFrame();
				}
			}
		}
		else if(_unitToControl->isCompleted() && _unitToControl->getType() == UnitTypes::Zerg_Drone)
		{
			_canWork = true;
		}		
	}
private:
	bool _canWork;
};